package com.auemall.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.auemall.constant.ApplicationConstant;
import com.auemall.po.Buyer;
import com.auemall.po.BuyerGrades;
import com.auemall.po.HomeProductPage;
import com.auemall.po.ProductBrand;
import com.auemall.po.ProductCategory;
import com.auemall.service.AttributeService;
import com.auemall.service.AttributeValuesService;
import com.auemall.service.BuyGradeService;
import com.auemall.service.BuyerService;
import com.auemall.service.HomeProductPageService;
import com.auemall.service.ProductBrandService;
import com.auemall.service.ProductCategoryService;
import com.auemall.service.ProductInfoService;
import com.auemall.service.impl.BuyerGradesServiceImpl;
import com.auemall.utils.CfgUtil;
import com.auemall.utils.IDUtils;
import com.auemall.utils.sms.AppConfig;
import com.auemall.utils.sms.ConfigLoader;
import com.auemall.utils.sms.MessageSend;


@Controller
public class FrontController {
	
	
	@Autowired
	private BuyerService buyService;
	
	@Autowired
	private ProductCategoryService productCategoryService;
	
	@Autowired
	private ProductBrandService productBrandService;

	@Autowired
	private ProductInfoService productInfoService;
	
	@Autowired
	private AttributeService attributeService;
	
	@Autowired
	private AttributeValuesService attributeValuesService;
	
	@Autowired
	private HomeProductPageService homeProductPageService;
	
	@Autowired
	private BuyGradeService BuyerGradesService;
	
	@RequestMapping("/front/index")
	public String showProduct(HttpServletRequest request){
		
		List<ProductCategory> list = productCategoryService.getProductCategoryChildrenModues(ApplicationConstant.DEFAULT_VALUE_0);
		
		request.setAttribute("productCategory",list);
		
		List<ProductBrand> brandLists = productBrandService.findHomeProductBrand(ApplicationConstant.DEFAULT_VALUE_1);
		
		request.setAttribute("brandlist",brandLists);
		
		List<HomeProductPage> productPages =  homeProductPageService.findAll();
		
		request.setAttribute("productLists",productPages);
		
		return "index";
	}
	
	//注册
	@RequestMapping("/front/reg")
	public String register() {
		
		return "user/userRegister";
	}
	
	//登录
	@RequestMapping("/front/login")
	
	public String login() {
		
		return "user/userLogin";
	}
	
	
	//发送短信
	@RequestMapping("/front/sendMessage")
	@ResponseBody
	public Map<String,String> sendMessage (@RequestBody String phone,HttpServletRequest request) {
		
		 Map<String,String> map = new HashMap<String,String>();
		
		 String sCode = IDUtils.genRandom(ApplicationConstant.SMS_CODE_LENGTH) ;
        
         AppConfig config = ConfigLoader.load(ConfigLoader.ConfigType.Message) ;
        
         MessageSend sender = new MessageSend(config) ;
         
         sender.addTo(phone);
         
         sender.setProject(ApplicationConstant.SMG_PROJECT);
         
         sender.addVar("code",sCode);
         
         sender.addVar("time","2分钟");
         
         sender.xsend();
         
         //將生产的验证码保存到session中
         HttpSession session = request.getSession();
         
         session.setAttribute("code", sCode);
		
         map.put("result", "1");
	    
		 return map;
	
	}
	
	@RequestMapping("/front/validateAccount")
	@ResponseBody
	public Map<String,String> validateAccount (@RequestBody String username,HttpServletRequest request) {
		
		Map<String,String> map = new HashMap<String,String>();
		
		Buyer b = new Buyer();
		b.setUsername(username);
		
	    Buyer buyer = this.buyService.findBuyerByCondition(b);
	    
	    if (buyer != null) {
	    	
	      map.put("result","1");
	      map.put("msg","账户信息已存在");
	      
	    }else {
	    	
	    	map.put("result","0");
	    	map.put("msg","账户信息不存在");
	    }
	    
	    return map;
		
	}
	
	
	
	//注冊信息
	@RequestMapping("/front/registerInfo")
	public String registerInfo (HttpServletRequest request,String username,String password,String phone,String email,String smscode) {
		
		try {
			if (username != null && password != null && phone!=null && email!=null) {
				
				String code = (String) request.getSession().getAttribute("code");
				
				if (code.equals(smscode)) {
					
					Buyer buyer = new Buyer();
					
					buyer.setUsername(username);
					buyer.setPassword(password);
					buyer.setEmail(email);
					buyer.setMobile(phone);
					
				    //根据会员积分表默认会员等级查询
					BuyerGrades level = this.BuyerGradesService.findBuyerDefaultLevel(ApplicationConstant.DEFAULT_VALUE_1);
					
					buyer.setLevel(level.getId());

					buyer.setSource(ApplicationConstant.DEFAULT_VALUE_0);  //来自于pc
					
					buyer.setRegtime(new Date());
					
					this.buyService.insertSelective(buyer);
					
					request.setAttribute("message","注册成功");
					
					return "user/userRegFinish";
				}
				
			}else {
				
				request.setAttribute("message","注册失败");
				
				return "user/userRegFinish";
			}
		} catch (Exception e) {
			
			e.printStackTrace();
			request.setAttribute("message",e.getMessage());
			return "user/userRegFinish";
			
		}
		
		return null;
		
	}
	
	
	//登录
	@RequestMapping("/front/loginValidate")
	public String loginValidate(String username,String password,HttpServletRequest request) {
		
		if (username != null && password != null) {
			
			//查詢賬账号和面
			
			Buyer buyer  = new Buyer();
			
			buyer.setUsername(username);
			buyer.setPassword(password);
			
			Buyer user = this.buyService.findBuyerByCondition(buyer);
			
			 if (user != null) {
				 
				 HttpSession session = request.getSession();
				 
				 session.setAttribute("user",user);
				 
				 return "redirect:/member/mycenter";
				 
			 }else {
				
				 return "user/userLogin";
			 }
			
		}else {
		
			return "user/userLogin";
		}
		
		
	}
	
}
